limsPerson.detailPage = {

    getBtnToolbar: function () {
        let _this = this;
        return {
            type: 'btnToolbar',
            items: [{
                type: 'button',
                text: 'GIKAM.BUTTON.BACK',
                icon: 'back',
                onClick: function () {
                    workspace.window.goBack();
                }
            }]
        }
    },

    getBaseInfoTab: function () {
        let _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'BASEMODULE.RM.PERSONS.TAB.BASE_INFO',
                items: [{
                    type: 'form',
                    service: 'limsPersonServiceImpl',
                    dbTable: 'T_LIMS_PERSON',
                    url: Gikam.printf(limsPerson.baseUrl + '/{id}', {
                        id: Gikam.isEmpty(_this.param.id) ? -1 : _this.param.id
                    }),
                    id: 'lims-person-edit-list-base-info-form',
                    fields: limsPerson.getBaseInfoFormFields(),
                    onUpdated: function () {
                        if (this.getData().id) {
                            let grid = Gikam.getComp('lims-person-edit-list-grid');
                            grid.refreshRowById(_this.param.id);
                        }
                    },
                }]
            }, {
                title: 'BASEMODULE.RM.PERSONS.TAB.CERTIFICATE',
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getCertificateGrid('lims-person-certificate-edit-list-grid')]
                    },
                    center: {
                        items: [_this.getAttachmantStruct('lims-person-certificate-edit-list-grid', 'T_LIMS_PERSON_CERTIFICATE')]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.PERSONS.TAB.ABILITY',
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getAbilityGrid('lims-person-ability-edit-list-grid')]
                    },
                    center: {
                        items: [_this.getAttachmantStruct('lims-person-ability-edit-list-grid', 'T_LIMS_PERSON_ABILITY')]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.PERSONS.TAB.EDUEXPERIENCE',//学习经历
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getEduExperienceGrid('lims-person-edu-experience-edit-list-grid')]
                    },
                    center: {
                        items: [_this.getAttachmantStruct('lims-person-edu-experience-edit-list-grid', 'T_LIMS_PERSON_EDU_EXPERIENCE')]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.PERSONS.TAB.WORKEXPERIENCE',//工作经历
                items: [{
                    type: 'layout',
                    north: {
                        height: '50%',
                        items: [_this.getWorkExperienceGrid('lims-person-work-experience-edit-list-grid')]
                    },
                    center: {
                        items: [_this.getAttachmantStruct('lims-person-work-experience-edit-list-grid', 'T_LIMS_PERSON_WORK_EXPERIENCE')]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.PERSONS.TAB.METHOD-CERTIFICATE',
                items: [{
                    type: 'grid',
                    id: 'lims-method-certificate--grid',
                    url: limsMethodCertificate.baseUrl + '/queries',
                    requestData: {
                        personName_EQ: _this.param.personName,
                        auditStatus_EQ: 'done'
                    },
                    page: true,
                    service: 'limsMethodCertificateServiceImpl',
                    dbTable: 'T_LIMS_METHOD_CERTIFICATE',
                    columns: limsMethodCertificate.getEditListColumns(),
                    readonly: true,
                }]
            }, {
                title: 'BASEMODULE.RM.PERSONS.TAB.EQUIP-CERTIFICATE',
                items: [{
                    type: 'grid',
                    id: 'lims-equipment-certificate--grid',
                    url: limsEquipmentCertificate.baseUrl + '/queries',
                    requestData: {
                        personName_EQ: _this.param.personName,
                        auditStatus_EQ: 'done'
                    },
                    page: true,
                    service: 'limsEquipmentCertificateServiceImpl',
                    dbTable: 'T_LIMS_EQUIPMENT_CERTIFICATE',
                    columns: limsEquipmentCertificate.getEditListColumns(),
                    readonly: true,
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    immediateUpload: true,
                    id: 'lims-person-edit-list-grid-uploader',
                    dbTable: 'T_LIMS_PERSON',
                    bizId: _this.param.id,
                    filter: false,
                    genericQuery: false,
                    onBeforeUpload: function () {
                        var activedRow = Gikam.getComp('lims-person-edit-list-grid').getActivedRow()
                        if (Gikam.isEmpty(activedRow)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                            return false
                        }
                    },
                }]
            }]
        }
    },

    getCertificateGrid: function (gridId) {
        let _this = this;
        return {
            type: 'grid',
            id: gridId,
            url: limsPersonCertificate.baseUrl + '/queries',
            requestData: {
                personId_EQ: _this.param.id
            },
            service: 'limsPersonCertificateServiceImpl',
            dbTable: 'T_LIMS_PERSON_CERTIFICATE',
            columns: limsPersonCertificate.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.certificateName || row.index + 1 || row.id
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            onBeforeUpdate: function (row, keys) {
                var grid = Gikam.getComp(gridId)
                var actRow = grid.getActivedRow()
                if (keys.includes('startDate') && row.startDate && row.startDate !== '') {
                    if (actRow.endDate && actRow.endDate !== '') {
                        var startDate = new Date(row.startDate);
                        var endDate = new Date(actRow.endDate);
                        if (startDate > endDate) {
                            Gikam.alert(Gikam.propI18N('BASEMODULE.RM.PERSONS.CERTIFICATE.TIP.STARTDATE_OVER_ENDDATE'))
                            grid.setData({
                                startDate: '',
                                id: actRow.id,
                                index: actRow.index
                            }, false)
                            return false
                        }
                    }
                }
                if (keys.includes('endDate') && row.endDate && row.endDate !== '') {
                    if (actRow.startDate && actRow.startDate !== '') {
                        var startDate = new Date(actRow.startDate);
                        var endDate = new Date(row.endDate);
                        if (startDate > endDate) {
                            Gikam.alert(Gikam.propI18N('BASEMODULE.RM.PERSONS.CERTIFICATE.TIP.STARTDATE_OVER_ENDDATE'))
                            grid.setData({
                                endDate: '',
                                id: actRow.id,
                                index: actRow.index
                            }, false)
                            return false
                        }
                    }
                }
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    if (Gikam.isEmpty(_this.param.id)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.PERSONS.MODAL.ADD',
                        fields: limsPersonCertificate.getBaseInfoFormFields(),
                        url: limsPersonCertificate.baseUrl,
                        param: {
                            personId: _this.param.id
                        }
                    }).done(function () {
                        Gikam.getComp(gridId).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(gridId).deleteRows(limsPersonCertificate.baseUrl)
                }
            }]
        }
    },

    getAbilityGrid: function (gridId) {
        let _this = this;
        return {
            type: 'grid',
            id: gridId,
            url: limsPersonAbility.baseUrl + '/queries',
            requestData: {
                personId_EQ: _this.param.id
            },
            service: 'limsPersonAbilityServiceImpl',
            dbTable: 'T_LIMS_PERSON_ABILITY',
            columns: limsPersonAbility.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.index + 1
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    if (Gikam.isEmpty(_this.param.id)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.PERSONS.MODAL.ADD',
                        fields: limsPersonAbility.getBaseInfoFormFields(),
                        url: limsPersonAbility.baseUrl,
                        param: {
                            personId: _this.param.id
                        }
                    }).done(function () {
                        Gikam.getComp(gridId).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(gridId).deleteRows(limsPersonAbility.baseUrl)
                }
            }]
        }
    },

    getEduExperienceGrid: function (gridId) {
        let _this = this;
        return {
            type: 'grid',
            id: gridId,
            url: limsPersonEduExperience.baseUrl + '/queries/raw',
            requestData: {
                personId_EQ: _this.param.id
            },
            service: 'limsPersonEduExperienceServiceImpl',
            dbTable: 'T_LIMS_PERSON_EDU_EXPERIENCE',
            columns: limsPersonEduExperience.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.index + 1
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    if (Gikam.isEmpty(_this.param.id)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.PERSONS.EDUEXPERIENCES.MODAL.ADD',
                        fields: limsPersonEduExperience.getBaseInfoFormFields(),
                        url: limsPersonEduExperience.baseUrl,
                        param: {
                            personId: _this.param.id
                        }
                    }).done(function () {
                        Gikam.getComp(gridId).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(gridId).deleteRows(limsPersonEduExperience.baseUrl)
                }
            }]
        }
    },

    getWorkExperienceGrid: function (gridId) {
        let _this = this;
        return {
            type: 'grid',
            id: gridId,
            url: limsPersonWorkExperience.baseUrl + '/queries/raw',
            requestData: {
                personId_EQ: _this.param.id
            },
            service: 'limsPersonWorkExperienceServiceImpl',
            dbTable: 'T_LIMS_PERSON_WORK_EXPERIENCE',
            columns: limsPersonWorkExperience.getEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.index + 1
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    if (Gikam.isEmpty(_this.param.id)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.PERSONS.WORKEXPERIENCES.MODAL.ADD',
                        fields: limsPersonWorkExperience.getBaseInfoFormFields(),
                        url: limsPersonWorkExperience.baseUrl,
                        param: {
                            personId: _this.param.id
                        }
                    }).done(function () {
                        Gikam.getComp(gridId).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(gridId).deleteRows(limsPersonWorkExperience.baseUrl)
                }
            }]
        }
    },

    getAttachmantStruct: function (gridId, dbTable) {
        return {
            type: 'tab',
            fill: true,
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    immediateUpload: true,
                    id: gridId + '-uploader',
                    dbTable: dbTable,
                    filter: false,
                    genericQuery: false,
                    onBeforeUpload: function () {
                        let activedRow = Gikam.getComp(gridId).getActivedRow();
                        if (Gikam.isEmpty(activedRow)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                            return false
                        }
                    },
                }]
            }]
        }
    },

    create: function () {
        let _this = this;
        Gikam.create('layout', {
            id: 'lims-person-detail-layout',
            center: {
                items: [_this.getBtnToolbar(), _this.getBaseInfoTab()]
            },
            renderTo: workspace.window.$dom
        });
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};